package com.huzz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huzz.entity.SysUser;
import com.huzz.mapper.SysLogMapper;
import com.huzz.mapper.SysUserMapper;
import com.huzz.model.SysLogModel;
import com.huzz.service.ISysLogService;
import com.huzz.util.entityutil.ModelUtil;
import com.huzz.util.log.SysLog;
import com.huzz.vo.SysLogVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* 系统日志-业务实现层
*
* @author 童年的纸飞机
* @since 2022-10-09 17:12
*/
@Service
@Slf4j
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements ISysLogService {

    @Autowired
    private SysLogMapper sysLogMapper;

    @Autowired
    private SysUserMapper sysUserMapper;


    @Override
    public IPage<SysLogVO> findSysLogListPage(Integer pageNum, Integer pageSize, SysLogModel model) {
        // 收集根据操作人信息查询的条件信息， 统一成根据操作人id查询
        List<Integer> userIds = new ArrayList<>();
        if (StringUtils.isNotEmpty(model.getOperateUsername())) {
            List<SysUser> sysUserList = sysUserMapper.selectList(Wrappers.<SysUser>lambdaQuery().like(SysUser::getUsername, model.getOperateUsername()));
            userIds = sysUserList.stream().map(u -> u.getId()).collect(Collectors.toList());
        }
        if (StringUtils.isNotEmpty(model.getOperateUserId())) {
            userIds.add(Integer.valueOf(model.getOperateUserId()));
        }
        // 封装查询条件
        LambdaQueryWrapper<SysLog> wrapper = Wrappers.<SysLog>lambdaQuery()
                .eq(StringUtils.isNotEmpty(model.getModuleName()), SysLog::getModuleName, model.getModuleName())
                .eq(StringUtils.isNotEmpty(model.getBusinessName()), SysLog::getBusinessName, model.getBusinessName())
                .eq(StringUtils.isNotEmpty(model.getRequestIp()), SysLog::getRequestIp, model.getRequestIp())
                .eq(StringUtils.isNotEmpty(model.getOperateType()), SysLog::getOperateType, model.getOperateType())
                .eq(null != model.getStatus(), SysLog::getStatus, model.getStatus())
                .in(!userIds.isEmpty(), SysLog::getOperateUserId, userIds)
                .orderByDesc(SysLog::getCreatedTime);
        if (null != model.getTimeInterval() && model.getTimeInterval().length == 2) {
            wrapper.between(SysLog::getCreatedTime, model.getTimeInterval()[0], model.getTimeInterval()[1]);
        }

        // 处理查询结果， 添加操作人姓名
        Page<SysLog> sysLogPage = sysLogMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        IPage<SysLogVO> voiPage = ModelUtil.transToSimilarPage(sysLogPage, SysLogVO.class);
        voiPage.getRecords().forEach(item -> {
            SysUser sysUser = sysUserMapper.selectById(item.getOperateUserId());
            if (null != sysUser) {
                item.setOperateUsername(sysUser.getUsername());
            }
        });

        return voiPage;
    }
}